package com.mvity.framework.common.service.impl;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.mvity.framework.common.annotation.ServiceDesc;
import com.mvity.framework.common.base.BaseEntity;
import com.mvity.framework.common.dao.BaseHibernateDao;
import com.mvity.framework.common.dao.BaseJdbcDao;
import com.mvity.framework.common.service.BaseService;

/**
 * Service 基类
 * 
 * @author vity
 * @date 2012-3-15
 * @mail vity.1215@gmail.com
 */
@Service(value = "baseService")
public class BaseServiceImpl implements Serializable, BaseService {

	private static final long serialVersionUID = -7348097272277240848L;

	protected static final Logger logger = LoggerFactory.getLogger(BaseServiceImpl.class);

	@Resource(name = "baseHibernateDao")
	protected BaseHibernateDao hibernateDAO;

	@Resource(name = "baseJdbcDao")
	protected BaseJdbcDao jdbcDAO;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.mvity.framework.common.service.impl.BaseService#save(T)
	 */
	@Override
	@ServiceDesc("保存对象")
	public <T extends BaseEntity> T save(T model) {
		return hibernateDAO.save(model);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.mvity.framework.common.service.impl.BaseService#saveOrUpdate(T)
	 */
	@Override
	@ServiceDesc("保存或更新对象")
	public <T extends BaseEntity> T saveOrUpdate(T model) {
		return hibernateDAO.saveOrUpdate(model);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.mvity.framework.common.service.impl.BaseService#update(T)
	 */
	@Override
	@ServiceDesc("更新对象")
	public <T extends BaseEntity> void update(T model) {
		hibernateDAO.update(model);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.mvity.framework.common.service.impl.BaseService#merge(T)
	 */
	@Override
	@ServiceDesc("merge 更新")
	public <T extends BaseEntity> void merge(T model) {
		hibernateDAO.merge(model);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.mvity.framework.common.service.impl.BaseService#delete(java.lang.
	 * Class, PK)
	 */
	@Override
	@ServiceDesc("删除对象")
	public <T extends BaseEntity, PK extends Serializable> void delete(Class<T> entityClass, PK id) {
		hibernateDAO.delete(entityClass, id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.mvity.framework.common.service.impl.BaseService#delete(T)
	 */
	@Override
	@ServiceDesc("删除对象")
	public <T extends BaseEntity> void delete(T model) {
		hibernateDAO.deleteObject(model);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.mvity.framework.common.service.impl.BaseService#get(java.lang.Class,
	 * PK)
	 */
	@Override
	@ServiceDesc("获取对象")
	public <T extends BaseEntity, PK extends Serializable> T get(Class<T> entityClass, PK id) {
		return hibernateDAO.get(entityClass, id);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.mvity.framework.common.service.impl.BaseService#countAll(java.lang
	 * .Class)
	 */
	@Override
	@ServiceDesc("获取集合总数")
	public <T extends BaseEntity> int countAll(Class<T> entityClass) {
		return hibernateDAO.countAll(entityClass);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.mvity.framework.common.service.impl.BaseService#listAll(java.lang
	 * .Class)
	 */
	@Override
	@ServiceDesc("获取集合")
	public <T extends BaseEntity> List<T> listAll(Class<T> entityClass) {
		return hibernateDAO.listAll(entityClass);
	}

}
